//
// webui-common
// Make any .NET app a Web Server « Brian Nickel’s Online Journal <http://kerrick.wordpress.com/2007/06/12/make-any-net-app-a-web-server/> 
//
import System
import System.IO
import System.Collections.Generic
import Banshee.ServiceStack
import Banshee.MediaEngine
import Banshee.PlaybackController
import Mono.Unix
import System.Reflection

import Boo.Lang.Compiler
import Boo.Lang.Compiler.IO
import Boo.Lang.Compiler.Pipelines
import Boo.Lang.Interpreter

import System.Web.Services
import System.Web.Services.Protocols

#region Common routine
def CombineMultiplePath( *paths as (string) ):
	result=""
	for path in paths:
		result=Path.Combine( result, path )
	return result
#endregion

#region Web Service
[WebService]
class BansheeWebService(WebService):
	[WebMethod]
	def Add(a as int, b as int):
		return a+b
		
	[WebMethod]
	def Multiply(a as int, b as int):
		return a*b
#endregion

#region Booc Helper
class BoocHelper:
	def Compile():
		myDocuments = Environment.GetFolderPath( Environment.SpecialFolder.MyDocuments )
		outputBinaryPath = CombineMultiplePath( myDocuments, ".config", "banshee-1", "boo-scripts", "webui", "bin" )

		if not Directory.Exists( outputBinaryPath ):
			Directory.CreateDirectory( outputBinaryPath )

		compiler = BooCompiler()
		compiler.Parameters.Ducky = true
		compiler.Parameters.Pipeline = CompileToFile()
		compiler.Parameters.Input.Add( FileInput( "${myDocuments}/.config/banshee-1/boo-scripts/webui-common.boo" ) )
		compiler.Parameters.OutputType = CompilerOutputType.Library
		compiler.Parameters.OutputAssembly = "${outputBinaryPath}/webui-common.dll"

		for assembly in AppDomain.CurrentDomain.GetAssemblies():
			compiler.Parameters.References.Add( assembly )

		context = compiler.Run()
        
		if context.GeneratedAssembly == null:
			Hyena.Log.Warning( join( context.Errors, Environment.NewLine ) ) if len( context.Errors )

#endregion

#region The bridge between Banshee and Web interface
interface IBansheePlayer:
	Playing as bool:
		get:
			pass
	
	NowPlayingDuration as TimeSpan:
		get:
			pass
	
	NowPlayingPosition as uint:
		get:
			pass
	
	NowPlayingArtist as string:
		get:
			pass
				
	NowPlayingAlbum as string:
		get:
			pass

	NowPlayingTitle as string:
		get:
			pass

	ShuffleMode as string:
		get:
			pass
		set:
			pass

	RepeatMode as string:
		get:
			pass
		set:
			pass

	def PlayNext():
		pass
		
	def PlayPrevious():
		pass

	def TogglePlaying():
		pass

class NullBansheePlayer( IBansheePlayer, IDisposable  ):
	Playing as bool:
		get:
			return false
	
	NowPlayingDuration as TimeSpan:
		get:
			return TimeSpan(0)
	
	NowPlayingPosition as uint:
		get:
			return 0
	
	NowPlayingArtist as string:
		get:
			return ""
				
	NowPlayingAlbum as string:
		get:
			return ""

	NowPlayingTitle as string:
		get:
			return ""

	ShuffleMode as string:
		get:
			return ""
		set:
			pass

	RepeatMode as string:
		get:
			return ""
		set:
			pass

	def PlayNext():
		pass
		
	def PlayPrevious():
		pass

	def TogglePlaying():
		pass

	def Dispose():
		Hyena.Log.Information( "NullBansheePlayer is recycled." )

class BansheePlayer( MarshalByRefObject, IBansheePlayer, IDisposable ):
	Playing as bool:
		get:
			return ServiceManager.PlayerEngine.CurrentState==PlayerState.Playing
	
	NowPlayingDuration as TimeSpan:
		get:
			if Playing:
				return ServiceManager.PlayerEngine.CurrentTrack.Duration
			else:
				return TimeSpan.Zero
	
	NowPlayingPosition as uint:
		get:
			if Playing:
				return ServiceManager.PlayerEngine.Position
			else:
				return 0
	
	NowPlayingArtist as string:
		get:
			if Playing:
				return ServiceManager.PlayerEngine.CurrentTrack.DisplayArtistName
			else:
				return ""
				
	NowPlayingAlbum as string:
		get:
			if Playing:
				return ServiceManager.PlayerEngine.CurrentTrack.DisplayAlbumTitle
			else:
				return ""

	NowPlayingTitle as string:
		get:
			if Playing:
				return ServiceManager.PlayerEngine.CurrentTrack.DisplayTrackTitle
			else:
				return ""

	ShuffleMode as string:
		get:
			return ServiceManager.PlaybackController.ShuffleMode.ToString()
		set:
			ServiceManager.PlaybackController.ShuffleMode = Enum.Parse( typeof( PlaybackShuffleMode ), value )

	RepeatMode as string:
		get:
			return ServiceManager.PlaybackController.RepeatMode.ToString()
		set:
			ServiceManager.PlaybackController.RepeatMode = Enum.Parse( typeof( PlaybackRepeatMode ), value )

	def PlayNext():
		ServiceManager.PlaybackController.Next()
		
	def PlayPrevious():
		ServiceManager.PlaybackController.Previous()

	def TogglePlaying():
		ServiceManager.PlayerEngine.TogglePlaying()
	
	def Dispose():
		Hyena.Log.Information( "BansheePlayer is recycled." )
#endregion

#region Useful functions
def GetShuffleModes() as IList[of string]:
		return List[of string]( Enum.GetNames( typeof(PlaybackShuffleMode) ) )

def GetRepeatModes() as IList[of string]:
		return List[of string]( Enum.GetNames( typeof(PlaybackRepeatMode) ) )
#endregion

Hyena.Log.Information( "webui-common script is loaded." )

helper = BoocHelper()
helper.Compile()

Hyena.Log.Information( "Compile done." )

